﻿using DeHeng_Mes.Constant;
using DeHeng_Mes.Model;
using DeHeng_Mes.Model.Data;
using DeHeng_Mes.Model.Table;
using DeHeng_Mes.Utils;
using Mysqlx.Crud;
using NPOI.SS.Formula.Functions;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Media.Animation;

namespace DeHeng_Mes.Dao
{
    public class OrderDao
    {

        #region
        #endregion

        /// <summary>
        /// 获取订单状态
        /// </summary>
        /// <param name="orderId"></param>
        /// <returns></returns>
        public bool GetOrderStatus(string orderId)
        {
            using (var db = SqlSugarConfig.Db)
            {
                return db.Queryable<OrderStatus>()
                      .Where(x => x.OrderId == orderId)
                      .Any();
            }
        }

        /// <summary>
        /// 获取订单信息
        /// </summary>
        /// <param name="orderId"></param>
        /// <returns></returns>
        public OrderStatus GetOrder(string orderId)
        {
            using (var db = SqlSugarConfig.Db)
            {
                return db.Queryable<OrderStatus>()
                    .Where(x => x.OrderId == orderId)
                    .First(); // 返回单个实体
            }
        }


        /// <summary>
        /// 插入新订单信息
        /// </summary>
        /// <param name="orderStatus"></param>
        /// <returns></returns>
        public int InsertOrder(OrderStatus orderStatus)
        {
            using (var db = SqlSugarConfig.Db)
            {
                return db.Insertable(orderStatus)
                  .ExecuteCommand();
            }
        }

        /// <summary>
        /// 完成订单修改对应信息
        /// </summary>
        /// <param name="orderStatus"></param>
        /// <returns></returns>
        public void UpdateOrder(OrderStatus orderStatus)
        {
            using (var db = SqlSugarConfig.Db)
            {
                db.Updateable<OrderStatus>()
                  .SetColumns(x => x.EndTime == orderStatus.EndTime)
                  .SetColumns(x => x.EndPower == orderStatus.EndPower)
                  .SetColumns(x => x.EndGas == orderStatus.EndGas)
                  .SetColumns(x => x.ProductionNg == orderStatus.ProductionNg)
                  .SetColumns(x => x.ProductionOk == orderStatus.ProductionOk)
                  .SetColumns(x => x.Status == orderStatus.Status)
                  .Where(x => x.OrderId == orderStatus.OrderId)
                  .ExecuteCommand();
            }
        }

        /// <summary>
        /// 订单开始，根据订单号更新订单状态与能耗
        /// </summary>
        /// <param name="orderStatus"></param>
        public void UpdateOrderStatus(OrderStatus orderStatus)
        {
            using (var db = SqlSugarConfig.Db)
            {
                db.Updateable<OrderStatus>()
                  .SetColumns(x => x.StartTime == orderStatus.StartTime)
                  .SetColumns(x => x.Status == orderStatus.Status)
                  .SetColumns(x => x.StartPower == orderStatus.StartPower)
                  .SetColumns(x => x.StartGas == orderStatus.StartGas)
                  .Where(x => x.OrderId == orderStatus.OrderId)
                  .ExecuteCommand();
            }
        }

        /// <summary>
        /// 更新订单结束状态
        /// </summary>
        /// <param name="orderStatus"></param>
        /// <exception cref="Exception"></exception>
        public void UpdateOrderEndStatus(OrderStatus orderStatus)
        {
            using (var db = SqlSugarConfig.Db)
            {
                db.Updateable<OrderStatus>()
                  .SetColumns(x => x.EndTime == orderStatus.EndTime)
                  .SetColumns(x => x.EndPower == orderStatus.EndPower)
                  .SetColumns(x => x.EndGas == orderStatus.EndGas)
                  .SetColumns(x => x.Status == orderStatus.Status)
                  .Where(x => x.OrderId == orderStatus.OrderId)
                  .ExecuteCommand();
            }
        }

        /// <summary>
        /// 更新追加后的计划数
        /// </summary>
        /// <param name="orderId"></param>
        /// <param name="produceNum"></param>
        /// <exception cref="NotImplementedException"></exception>
        public void AdditionalOrder(string orderId, int produceNum)
        {
            using (var db = SqlSugarConfig.Db)
            {
                db.Updateable<OrderStatus>()
                  .SetColumns(x => x.ProductionPlan == produceNum.ToString())
                  .Where(x => x.OrderId == orderId)
                  .ExecuteCommand();
            }
        }

        /// <summary>
        /// 更新订单开始时间
        /// </summary>
        /// <param name="id"></param>
        /// <param name="now"></param>
        /// <exception cref="NotImplementedException"></exception>
        public void UpdateStartTime(int id, DateTime now)
        {
            using (var db = SqlSugarConfig.Db)
            {
                db.Updateable<OrderStatus>()
                  .SetColumns(x => x.StartTime == now)
                  .Where(x => x.Id == id)
                  .ExecuteCommand();
            }
        }

        /// <summary>
        /// 数据读取不为ok并且未创建的数据
        /// </summary>
        /// <param name="ok"></param>
        public List<OrderTrace> dataIsSave(string ok)
        {
            using (var db = SqlSugarConfig.Db)
            {
                return db.Queryable<OrderTrace>()
                         .Where(x => x.IsSave != SystemConstant.Ok)
                         .ToList();
            }
        }

        /// <summary>
        /// 更新追溯码或者总成码为Code的字段状态为未创建
        /// </summary>
        /// <param name="code"></param>
     /*   public void UpdateDataStatusNCreate(string code)
        {
            using (var db = SqlSugarConfig.Db)
            {
                db.Updateable<OrderTrace>()
                  .SetColumns(x => x.IsSave == SystemConstant.nCreate.ToString())
                  .Where(x => x.TraceId == code || x.FictitiousCode == code)
                  .ExecuteCommand();
            }
        }*/

        /*public void UpdateStatusOkByCode(string code)
        {
            using (var db = SqlSugarConfig.Db)
            {
                db.Updateable<OrderTrace>()
                  .SetColumns(x => x.IsSave == "1")
                  .Where(x => x.TraceId == code || x.FictitiousCode == code)
                  .ExecuteCommand();
            }
        }*/

        /// <summary>
        /// 查找数据库是否包含该订单号的数据
        /// </summary>
        /// <param name="orderId"></param>
        /// <returns></returns>
        public bool OrderIsSave(string orderId)
        {
            using (var db = SqlSugarConfig.Db)
            {
                return db.Queryable<OrderStatus>()
                    .Where(x => x.OrderId == orderId)
                    .Any();
            }
        }

        /// <summary>
        /// 根据订单号获取PlcName字段
        /// </summary>
        /// <param name="orderId"></param>
        /// <returns></returns>
        public string GetLineType(string orderId)
        {
            using (var db = SqlSugarConfig.Db)
            {
                return db.Queryable<OrderStatus>()
                    .Where(x => x.OrderId == orderId)
                    .Select(x => x.PlcName)
                    .First();
            }
        }

        /// <summary>
        /// 根据订单号获取PartId字段
        /// </summary>
        /// <param name="orderId"></param>
        /// <returns></returns>
        public string GetActualLineType(string orderId)
        {
            using (var db = SqlSugarConfig.Db)
            {
                return db.Queryable<OrderStatus>()
                    .Where(x => x.OrderId == orderId)
                    .Select(x => x.MaterialCode)
                    .First();
            }
        }

        /// <summary>
        /// 更新追溯码或者总成码为Code的字段状态为2
        /// </summary>
        /// <param name="code"></param>
      /*  public void UpdateDataStatusOk(int code)
        {
            using (var db = SqlSugarConfig.Db)
            {
                db.Updateable<OrderTrace>()
                  .SetColumns(x => x.IsSave == "1")
                  .Where(x => x.Id == code)
                  .ExecuteCommand();
            }
        }*/


        /// <summary>
        /// 更新主键或者总成码为Code的字段状态为-1
        /// </summary>
        /// <param name="code"></param>
  /*      public void UpdateDataStatusIllegal(int code)
        {
            using (var db = SqlSugarConfig.Db)
            {
                db.Updateable<OrderTrace>()
                  .SetColumns(x => x.IsSave == SystemConstant.illegal)
                .Where(x => x.Id == code)
                  .ExecuteCommand();
            }
        }*/

        /// <summary>
        /// 更新追溯码或者总成码为Code的字段状态为未创建
        /// </summary>
        /// <param name="code"></param>
      /*  public void UpdateDataStatusNCreate(int code)
        {
            using (var db = SqlSugarConfig.Db)
            {
                db.Updateable<OrderTrace>()
                  .SetColumns(x => x.IsSave == SystemConstant.nCreate)
                  .Where(x => x.Id == code)
                  .ExecuteCommand();
            }
        }*/

        /// <summary>
        /// 更新Plc名称，订单状态，订单开始生产时间、开始耗电量、开始耗气量
        /// </summary>
        /// <param name="orderStatus"></param>
        public void UpdateStartOrder(OrderStatus orderStatus)
        {
            using (var db = SqlSugarConfig.Db)
            {
                db.Updateable<OrderStatus>()
                  .SetColumns(x => x.Status == SystemConstant.Ok)
                  .SetColumns(x => x.PlcName == orderStatus.PlcName)
                  .SetColumns(x => x.StartTime == DateTime.Now)
                  .SetColumns(x => x.StartPower == orderStatus.StartPower)
                  .SetColumns(x => x.StartGas == orderStatus.StartGas)
                  .Where(x => x.OrderId == orderStatus.OrderId)
                  .ExecuteCommand();
            }
        }

        /// <summary>
        /// 根据主键更新开始时间推送状态
        /// </summary>
        /// <param name="id"></param>
        public void UpdateStartTimeStatus(int id)
        {
            using (var db = SqlSugarConfig.Db)
            {
                db.Updateable<OrderStatus>()
                  .SetColumns(x => x.StartWorkStatus == SystemConstant.Ok)
                  .Where(x => x.Id == id)
                  .ExecuteCommand();
            }
        }
        /// <summary>
        /// 根据虚拟码查询OrderTrace表中TraceId字段，如果虚拟码不存在，则虚拟码的值即为TraceId的值
        /// </summary>
        /// <param name="fictitiousCode"></param>
        /// <returns></returns>
        public string CheckTraceIdByFictitiousCode(string fictitiousCode)
        {
            using (var db = SqlSugarConfig.Db)
            {
                string traceId;
                List<OrderTrace> list = db.Queryable<OrderTrace>()
                                .Where(x => x.TraceId == fictitiousCode)
                                .ToList();
                if (list.Count > 0)
                {
                    traceId = fictitiousCode;
                }
                else
                {
                    traceId = db.Queryable<OrderTrace>()
                        .Where(x => x.FictitiousCode == fictitiousCode)
                        .Select(x => x.TraceId)
                        .First();
                }
                return traceId;
            }
        }

        public void UpdateIsSaveByTraceId(string traceId)
        {
            using (var db = SqlSugarConfig.Db)
            {
                db.Updateable<OrderTrace>()
                    .SetColumns(x => x.IsSave == SystemConstant.Ok)
                    .Where(x => x.TraceId == traceId)
                    .ExecuteCommand();
            }
        }

        public int selectOrderTraceCount(string traceId)
        {
            using (var db = SqlSugarConfig.Db)
            {
                return db.Queryable<OrderTrace>()
                         .Where(x => x.TraceId == traceId)
                         .Sum(x => x.SaveCount);
            }
        }

        public void DeleteOrderTraceByTraceId(string traceId)
        {
            using (var db = SqlSugarConfig.Db)
            {
                db.Deleteable<OrderTrace>()
                    .Where(x => x.TraceId == traceId)
                    .ExecuteCommand();
            }
        }

        public void DeleteOrder(string orderId)
        {
            using (var db = SqlSugarConfig.Db)
            {
                int count = db.Deleteable<OrderStatus>()
                              .Where(x => x.OrderId == orderId)
                              .ExecuteCommand();

                if (count > 0)
                {
                    Tools.ProduceLogText($"{orderId}执行回滚操作");
                }
            }
        }
    }
}
